package com.mlt.back.web.servlet;

import com.mlt.back.beans.BasicInfo;
import com.mlt.back.service.BasicInfoService;
import com.mlt.back.service.impl.BasicInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet({"/basicInfo/add","/basicInfo/modify","/basicInfo/remove"})
public class BasicInfoServlet extends HttpServlet{
    private BasicInfoService basicInfoService = new BasicInfoServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String servletPath = req.getServletPath();
        req.setCharacterEncoding("UTF-8");
        //添加
        if ("/basicInfo/add".equals(servletPath)){
            doBasicInfoAdd(req, resp);
        }
        //修改
        if ("/basicInfo/modify".equals(servletPath)){
            doBasicInfoUpdate(req,resp);
        }
        //删除
        if ("/basicInfo/remove".equals(servletPath)){
            doBasicInfoDelete(req,resp);
        }
    }
    //添加
    private void doBasicInfoAdd(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        // 获取前端传值
        String basic_info_name = req.getParameter("basic_info_name");
        // 构造实体类
        BasicInfo basicInfo = new BasicInfo(0, basic_info_name, 1);
        // 执行添加
        Integer add = basicInfoService.add(basicInfo);
        // 处理响应中文
        resp.setContentType("text/html;charset=utf-8");
        // 判断回传
        if (add > 0){
            resp.getWriter().print("<script>alert('添加成功');location.href='/mlt_war_exploded/basic_info_list.jsp'</script>");
        }else {
            resp.getWriter().print("<script>alert('添加失败');location.href='/mlt_war_exploded/add_basic_info.jsp'</script>");
        }

    }
    //修改
    private void doBasicInfoUpdate(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException{
        // 获取前端传值
        String basic_info_id = req.getParameter("basic_info_id");
        String basic_info_name = req.getParameter("basic_info_name");
        String basic_info_status = req.getParameter("basic_info_status");
        // 构造实体类
        BasicInfo basicInfo = new BasicInfo(Integer.parseInt(basic_info_id), basic_info_name, Integer.parseInt(basic_info_status));
        // 执行更新
        Integer modify = basicInfoService.modify(basicInfo);
        // 处理响应中文
        resp.setContentType("text/html;charset=utf-8");
        // 判断回传
        if (modify > 0){
            resp.getWriter().print("<script>alert('更新成功');location.href='/mlt_war_exploded/basic_info_list.jsp'</script>");
        }else {
            resp.getWriter().print("<script>alert('更新失败');location.href='/mlt_war_exploded/basic_info_list.jsp'</script>");
        }
    }
    //删除
    private void doBasicInfoDelete(HttpServletRequest req, HttpServletResponse resp)
            throws IOException{

        // 获取前端传值
        String id = req.getParameter("id");
        // 执行删除
        Integer remove = basicInfoService.remove(Integer.parseInt(id));
        // 处理响应中文
        resp.setContentType("text/html;charset=utf-8");
        // 判断回传
        if (remove > 0){
            resp.getWriter().print("<script>alert('删除成功');location.href='/mlt_war_exploded/basic_info_list.jsp'</script>");
        }else {
            resp.getWriter().print("<script>alert('删除失败');location.href='/mlt_war_exploded/basic_info_list.jsp'</script>");
        }

    }
}

